#import "MapBuildingSearchView.h"

// Singleton instance of MapBuildingSearchView
static MapBuildingSearchView* sharedMapBuildingSearchView = nil;

/**
 * A MapBuildingSearchView is an object representing the view that 
 * displays a list of buildings by which to search for on the map.
 *
 * A MapBuildingSearchDelegate must be set to respond to events from
 * the map building search viewer.
 *
 * @author      James Lin
 * @version     0.8.2
 * @since       0.8.2
 * @see         MapBuildingSearchDelegate
 */
@implementation MapBuildingSearchView

/**
 * Retrieve the shared instance of the map building search view.
 *
 * @return  the shared instance of MapBuildingSearchView
 */
+ (MapBuildingSearchView*)sharedMapBuildingSearchView {
	@synchronized(self) {
		if (sharedMapBuildingSearchView == nil) {
			sharedMapBuildingSearchView = [[MapBuildingSearchView alloc] init];
		}
		return sharedMapBuildingSearchView;
	}
}

/**
 * Allocate the map building search view.
 *
 * @return  the newly allocated shared instance of MapBuildingSearchView
 */
+ (id) alloc {
	@synchronized(self) {
		sharedMapBuildingSearchView = [super alloc];
		return sharedMapBuildingSearchView;
	}
}

/**
 * Retrieve a copy of the map building search view.
 *
 * @return  the shared instance of MapBuildingSearchView
 */
+ (id) copy {
	@synchronized(self) {
		return sharedMapBuildingSearchView;
	}
}

/**
 * Get the delegate handling events from this view.
 *
 * @return  the delegate handling events from this view
 * @see     MapGenericSearchView
 */
- (id<MapBuildingSearchDelegate>)delegate {
	return delegate;
}

/**
 * Set the delegate handling events from this view.
 *
 * @param theDelegate  the delegate handling events from this view
 * @see                MapGenericSearchView
 */
- (void)setDelegate:(id<MapBuildingSearchDelegate>)theDelegate {
	delegate = theDelegate;
}

/**
 * Display the table listing of buildings in a new view.
 *
 * Pushes this view onto the navigation controller stack of the parent
 * view. Upon tapping the back button, the view is automatically 
 * popped off the top of the navigation stack.
 *
 * @param buildings      the list of buildings to display
 * @param navController  the navigation controller of the parent view
 */
- (void)displayViewWithBuildings:(NSArray*)buildings 
				   navController:(UINavigationController*)navController {
	// Create the table of buildings
	buildingTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 
																	  VIEW_WIDTH, 
																	  VIEW_HEIGHT) 
													 style:UITableViewStylePlain];
	buildingTableView.backgroundColor = [UIColor lightGrayColor];
	buildingTableView.separatorColor = [UIColor whiteColor];
	buildingTableView.tableFooterView = [[[UIView alloc] 
										  initWithFrame:CGRectMake(0, 0, 1, 60)] autorelease];
	[buildingTableView setRowHeight:BUILDING_CELL_HEIGHT];
	[buildingTableView setDelegate:self];
	[buildingTableView setDataSource:self];
	self.navigationItem.title = @"Buildings";
	parentNav = navController;
	
	// Populate the table with buildings
	if (buildingList) {
		[buildingList release];
	}
	buildingList = [[NSArray alloc] initWithArray:buildings];
	[buildingTableView reloadData];
	
	// Display the table of buildings
	[self.view addSubview:buildingTableView];
	[parentNav pushViewController:self animated:YES];
}

/**
 * Get the number of rows in the table view.
 */
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
	return buildingList.count;
}

/**
 * Populate a table view cell in the table view with content.
 */
- (UITableViewCell*)tableView:(UITableView*)tableView 
		cellForRowAtIndexPath:(NSIndexPath*)indexPath {
	static NSString* CellIdentifier = @"BuildingSearchCell";
	UITableViewCell* cell = [buildingTableView dequeueReusableCellWithIdentifier:CellIdentifier];
	
	if (cell == nil) {
		// Create an empty reusable cell for performance sake
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
									   reuseIdentifier:CellIdentifier] autorelease];

		// Set the background color for the cell when selected
		UIView* cellView = [[[UIView alloc] init] autorelease];
		cellView.backgroundColor = [UIColor darkGrayColor];
		cell.selectedBackgroundView = cellView;
	}
	
	// Refresh the label content of the cell
	cell.textLabel.text = ((Building*)[buildingList objectAtIndex:indexPath.row]).name;
	cell.textLabel.font = [UIFont boldSystemFontOfSize:13.5];
	
	return cell;
}

/**
 * Search for the building that is selected.
 */
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	[delegate searchRequestByBuilding:(Building*)[buildingList objectAtIndex:indexPath.row] 
								isNew:YES];
	[parentNav popViewControllerAnimated:YES];
}

/**
 * Deallocate the building search view.
 */
- (void)dealloc {
	[buildingList release];
	[buildingTableView release];
	[sharedMapBuildingSearchView release];
    [super dealloc];
}

@end
